<?php

!defined('IN_ROOT') && exit('Access Denied');

class catemodel {

    var $db;
    var $base;

    function catemodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }


    /*获取分类信息*/
    function get($id) {
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."cate WHERE id='$id'");
    }
    function get_list($tid) {
        $catelist = array();
        $query = $this->db->query("SELECT * FROM ".DB_TABLEPRE."cate where tid='$tid' order by views asc");
        while($cate = $this->db->fetch_array($query)) {
            $catelist[] = $cate;
        }
        return $catelist;
    }
    function get_syscatename($id)
	{
		return $this->db->result_first("SELECT name FROM `".DB_TABLEPRE."catesys` where id='$id'");
	}
	function get_catename($id)
	{
		return $this->db->result_first("SELECT name FROM `".DB_TABLEPRE."cate` where id='$id'");
	}
	function get_cateshow($id,$type,$lid) {
		$larray = array();
		if(is_numeric($lid)){
			$lq = $this->db->query("SELECT `cid` FROM `".DB_TABLEPRE."maplinecate` where `lid`='$lid'");
			while($cd = $this->db->fetch_array($lq)) {
	            $larray[] = $cd['cid'];
	        }
		}
		//var_dump($lid);
		//var_dump("SELECT `cid` FROM `".DB_TABLEPRE."maplinecate` where `lid`='$lid'");
		$catelist = $this->get_list($id);
		$str = '';
		$br = 6;
		$i = 0;
		if($type == 1) {//单选
			
			foreach ($catelist as $v) {
				if(in_array($v['id'],$larray)){
					$ck = ' checked';
				}else {
					$ck = '';
				}
				
				$str .= '&nbsp;&nbsp;<input type="radio" value="'.$v['id'].'" name="cate_'.$id.'[]"'.$ck.'>'.$v['name'];
				$i++;
				if($i%$br ==0){
					$str .= '<br />';
				}
			}
		}
		elseif ($type == 2) {//多选
			foreach ($catelist as $v) {
				if(in_array($v['id'],$larray)){
					$ck = ' checked';
				}else {
					$ck = '';
				}
				$str .= '&nbsp;&nbsp;<input type="checkbox" value="'.$v['id'].'" name="cate_'.$id.'[]"'.$ck.'>'.$v['name'];
				$i++;
				if($i%$br ==0){
					$str .= '<br />';
				}
			}
		} else {//下拉
			$str .= '<select name="cate_'.$id.'[]">';
			foreach ($catelist as $v) {
				if(in_array($v['id'],$larray)){
					$ck = ' selected';
				}else {
					$ck = '';
				}
				$str .= '<option value="'.$v['id'].'"'.$ck.'>'.$v['name'].'</option>';
			}
			$str .= '</select>';
		}
		return $str;
	}
	function get_cateshow_admin($lid='') {
		
		$query = $this->db->query("SELECT `id`,`name`,`type` FROM `".DB_TABLEPRE."catesys` where `mid`=1 order by views asc");
		$str = '';
        while($t = $this->db->fetch_array($query)) {
            $d= $this->get_cateshow($t['id'],$t['type'],$lid);
            $str .= '<tr><th><font color="Red">*</font>'.$t['name'].'<input type="hidden" name="topid['.$t['id'].']" value="'.$t['name'].'"></th></tr>';
            $str .= '<tr><td>'.$d.'</td></tr>';
        }
        return $str;
	}
	function get_cateshow_user($lid='') {
		
		$query = $this->db->query("SELECT `id`,`name`,`type` FROM `".DB_TABLEPRE."catesys` where `mid`=1 order by views asc");
		$str = '';
        while($t = $this->db->fetch_array($query)) {
            $d= $this->get_cateshow($t['id'],$t['type'],$lid);
            $str .= '<DL><DT>'.$t['name'].'：<EM>*</EM></DT><DD class="inputclearboth"><input type="hidden" name="topid['.$t['id'].']" value="'.$t['name'].'">'.$d.'</DD></DL>';
        }
        return $str;
	}
    function list_catesys() {
    	$categorylist = array();
        $query = $this->db->query("SELECT * FROM `".DB_TABLEPRE."catesys` order by views asc");
        while($category = $this->db->fetch_array($query)) {
            $categorylist[] = $category;
        }
        return $categorylist;
    }
    function addsyscate($name,$type,$mid)
	{
		$this->db->query('INSERT INTO '.DB_TABLEPRE."catesys(`name`,`type`,`mid`) values ('$name','$type','$mid')");
        return $this->db->insert_id();
	}
	function catelist_byid($id)
	{
		$categorylist = array();
        $query = $this->db->query("SELECT * FROM `".DB_TABLEPRE."cate` where tid='$id' order by views asc");
        while($category = $this->db->fetch_array($query)) {
            $categorylist[] = $category;
        }
        return $categorylist;
	}
	
	function addcid($lines,$pid=0,$displayorder=0)
	{
        $sql = "INSERT INTO `".DB_TABLEPRE."cate`(`name` ,`tid` ,`views`) VALUES ";
        foreach($lines as $line) {
            $line=str_replace(array("\r\n", "\n", "\r"), '', $line);
            if(empty($line))continue;
            @list($name,$categorydir)=explode('，' , $line);
            $sql .= "('$name','$pid',$displayorder),";
            $displayorder++;
        }
        $sql=substr($sql,0,-1);
        return $this->db->query($sql);
	}
	function get_syscate_byid($id) 
	{
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."catesys WHERE id='$id'");
    }
    function updatesyscate($name,$type,$mid,$id)
	{
		$this->db->query("UPDATE `".DB_TABLEPRE."catesys` SET  `name`='$name' ,`type`='$type',`mid`='$mid' WHERE `id`=$id");
	}
	function upcid($cids)
	{
		if(is_array($cids))
		{
			foreach ($cids as $k => $v)
			{
				!empty($v) && $this->db->query("UPDATE `".DB_TABLEPRE."cate` SET  `name`='$v' WHERE `id`=$k");
			}
		}
	}
	function order_cate($id,$order) 
	{
        $this->db->query("UPDATE `".DB_TABLEPRE."cate` SET 	`views` = '{$order}' WHERE `id` = '{$id}'");
    }
    function order_catesys($id,$order) 
	{
        $this->db->query("UPDATE `".DB_TABLEPRE."catesys` SET 	`views` = '{$order}' WHERE `id` = '{$id}'");
    }
    function delcid($id)
    {
		$this->db->query("DELETE FROM `".DB_TABLEPRE."cate` WHERE `id`='$id'");
	}
	function delsyscid($id)
	{
		$this->db->query("DELETE FROM `".DB_TABLEPRE."cate` WHERE `tid`='$id'");
		$this->db->query("DELETE FROM `".DB_TABLEPRE."catesys` WHERE `id`='$id'");
	}
	
	
	function list_cateout()
	{
		$categorylist = array();
        $query = $this->db->query("SELECT * FROM `".DB_TABLEPRE."cateconfig`");
        while($category = $this->db->fetch_array($query)) {
            $categorylist[] = $category;
        }
        return $categorylist;
	}
	function addoutcate($name)
	{
		$this->db->query('INSERT INTO '.DB_TABLEPRE."cateconfig(`name`) values ('$name')");
        return $this->db->insert_id();
	}
	function deloutcid($id)
	{
		$this->db->query("DELETE FROM `".DB_TABLEPRE."cateout` WHERE `tid`='$id'");
		$this->db->query("DELETE FROM `".DB_TABLEPRE."cateconfig` WHERE `id`='$id'");
	}
	function catelist_byoutid($id)
	{
		$categorylist = array();
        $query = $this->db->query("SELECT * FROM `".DB_TABLEPRE."cateout` where tid='$id' order by views asc");
        while($category = $this->db->fetch_array($query)) {
            $categorylist[] = $category;
        }
        return $categorylist;
	}
	function get_outcatename($id)
	{
		return $this->db->result_first("SELECT name FROM `".DB_TABLEPRE."cateconfig` where id='$id'");
	}
	function addcidout($lines,$pid=0,$displayorder=0)
	{
        $sql = "INSERT INTO `".DB_TABLEPRE."cateout`(`name` ,`tid` ,`views`) VALUES ";
        foreach($lines as $line) {
            $line=str_replace(array("\r\n", "\n", "\r"), '', $line);
            if(empty($line))continue;
            @list($name,$categorydir)=explode('，' , $line);
            $sql .= "('$name','$pid',$displayorder),";
            $displayorder++;
        }
        $sql=substr($sql,0,-1);
        return $this->db->query($sql);
	}
	function get_outcate_byid($id) 
	{
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."cateconfig WHERE id='$id'");
    }
    function updateoutcate($name,$id)
	{
		$this->db->query("UPDATE `".DB_TABLEPRE."cateconfig` SET  `name`='$name' WHERE `id`=$id");
	}
	function order_cateout($id,$order) 
	{
        $this->db->query("UPDATE `".DB_TABLEPRE."cateout` SET 	`views` = '{$order}' WHERE `id` = '{$id}'");
    }
    function upcidout($cids)
	{
		if(is_array($cids))
		{
			foreach ($cids as $k => $v)
			{
				!empty($v) && $this->db->query("UPDATE `".DB_TABLEPRE."cateout` SET  `name`='$v' WHERE `id`=$k");
			}
		}
	}
	function delcidout($id)
    {
		$this->db->query("DELETE FROM `".DB_TABLEPRE."cateout` WHERE `id`='$id'");
	}
}
?>
